package br.com.techfor.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.techfor.dao.FuncionarioDAO;
import br.com.techfor.model.Funcionario;

/**
 * 
 * @author alex.amaral
 * 
 */
public class ConsultaFuncionario extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */

	
	public ConsultaFuncionario() {
		super();
		
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		FuncionarioDAO fdao = new FuncionarioDAO();

		String palavra = request.getParameter("palavra");

		List<Funcionario> funcionarios = fdao.existe(palavra);

		if (funcionarios.size() > 0) {
			request.setAttribute("funcionarios", funcionarios);
			RequestDispatcher rd = request
					.getRequestDispatcher("/listarfuncionario.jsp");
			rd.forward(request, response);
		} else {
			
			RequestDispatcher rd = request.getRequestDispatcher("/consultafuncionario.jsp");
			rd.forward(request, response);

		}

	}

}
